import React from 'react'
import { connect } from 'react-redux'
import { getadd, gettion } from '../../actions/actions'
//import * as counterActions from '../../actions/actions'
import '../../style/login.css'
import { Button, Form, Input, message } from 'antd'
import { UserOutlined, LockOutlined } from '@ant-design/icons';
import axios from 'axios'
//import axios from 'a'
//import c from '../../index'
//import {bindActionCreators} from 'redux'
class Login extends React.Component {
    //     NormalLoginForm = () => {
    //         onFinish = values => {
    //            console.log('Received values of form: ', values)
    //        }
    //    }
    constructor(props) {
        super(props)
        this.state = {
            username: '',
            password: ''
        }
    }
    componentDidMount() {
        // console.log('我有回来了')
    }
    success = (v) => {
        message.success(v);
    }
    error = (v) => {
        message.error(v);
    }
    onChange = (e) => {
        this.setState({
            [e.target.name]: e.target.value
        })
        // console.log(this.state.username,this.state.password)
    }
    submit = () => {
        let data = {
            user: this.state.username,
            password: this.state.password
        }
        console.log(this.axios)
        axios.post('https://dbyxs.xyz:8002/users/login', data).then(res => {
            if (res.data.err_code !== 1) {
                this.success('登录成功')
                this.props.getadd()
                // c()
                window.sessionStorage.setItem('token', res.data.jwt)
                window.sessionStorage.setItem('Time', +new Date())
                this.props.history.push('/echarts')
                this.props.history.go()
            } else {
                this.error('账号或密码错误')
            }
        })



    }
    getadd = () => {
        let data = {
            user: this.state.username,
            password: this.state.password
        }
        console.log(this.state.username)
        axios.post('https://dbyxs.xyz:8002/getuser', data).then(res => {
            //console.log(res)
            if (res.data.err_code === 1) {
                // axios.post('https://dbyxs.xyz:8002/user',data).then(res=>{

                // })
            } else {
                this.error('抱歉此用户名已注册')
            }
        })
        // this.success('注册成功')
    }
    render() {
        // console.log(this.props,'没有islogin')

        // store.subscribe(this.render)
        // const { getadd, gettion} = this.props
        return (
            <div className='main'>
                {/* <h1>我是登录页面来的{this.props.counter}</h1> */}
                {/* <Button type="primary" onClick={() => (getadd())}>增加</Button>
                <Button danger onClick={() => (gettion(5))}>减少</Button> */}
                {/* <Button type="primary" onClick={this.props.counterActions.getadd()}>增加</Button>
                <Button  danger onClick={this.props.counterActions.getmove()}>减少</Button> */}
                <Form
                    name="normal_login"
                    className="login-form"
                    initialValues={{ remember: true }}
                    onFinish={this.submit}
                >
                    <Form.Item
                        name="username"
                        rules={[{ required: true, message: '请输入用户名' }]}
                    >
                        <Input prefix={<UserOutlined className="site-form-item-icon" />} placeholder="用户名" onChange={this.onChange} name="username" style={{ minWidth: '300px' }} />
                    </Form.Item>
                    <Form.Item
                        name="password"
                        rules={[{ required: true, message: '请输入密码' }]}
                    >
                        <Input
                            prefix={<LockOutlined className="site-form-item-icon" />}
                            type="password"
                            placeholder="Password"
                            onChange={this.onChange} name="password"
                        />
                    </Form.Item>

                    <Form.Item>
                        <Button type="primary" htmlType="submit" className="login-form-button">
                            登录
                     </Button>
                        <Button type="primary" className="login-form-button" style={{ marginLeft: '20px' }} htmlType="getadd">注册</Button>
                    </Form.Item>
                </Form>
            </div>

        )
    }
}
const mapStateToProps = (state) => {
    return {
        counter: state
    }
}
const mapDispatchToProps = (dispatch) => {
    return {
        getadd: () => { dispatch(getadd()) },
        gettion: () => { dispatch(gettion()) }
    }
}
// const mapDispatchToProps=(dispatch)=>{
//     return{
//         counterActions:bindActionCreators(counterActions,dispatch)
//     }
// }
export default connect(mapStateToProps, mapDispatchToProps)(Login)
//export default Login